package vlms.servlet;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vlms.bean.Movie;
import vlms.bean.Person;
import vlms.bean.SimpleCustomer;
import vlms.service.VLMSServiceProxy;

/**
 * Servlet implementation class HandleDisplayMovie
 */
public class HandleDisplayMovie extends HttpServlet {
	private static final long serialVersionUID = 1L;
	VLMSServiceProxy proxy = new VLMSServiceProxy(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HandleDisplayMovie() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
		proxy.setEndpoint("http://localhost:8080/VLMS/services/VLMSService");
		
		HttpSession session = request.getSession(true);
		
		String movieId=request.getParameter("hidden");
		
		if(request.getParameter("submit").equals("Delete"))
		{
			String message=proxy.deleteMovie(movieId);
		
		session.setAttribute("message", message);
		
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/displayAllMovie.jsp");
		dispatcher.forward(request, response);
		}
		else
		{
			Movie movie=proxy.retrieveMovieById(movieId);
			
			String[] result= proxy.retrievePersonsByMovieId(movieId);
			for(String r:result)
			{
				System.out.println(r);
			}
			Person p[]=null;
			SimpleCustomer sc[]=null;
			if(result!=null)
			{
				int size=result.length;
				p=new Person[size];
				sc=new SimpleCustomer[size];
				int index=0;
				for(String temp: result)
				{
					
			StringTokenizer str=new StringTokenizer(temp,":");
			
			if(str.countTokens()==9)
			{
				System.out.println(str.countTokens());
				
				System.out.println("size in result" + size);
				
				p[index]=new Person();
				sc[index]=new SimpleCustomer();
				p[index].setPersonid(Integer.parseInt(str.nextToken()));
				p[index].setEmailid(str.nextToken());
				p[index].setFirstname(str.nextToken());
				p[index].setLastname(str.nextToken());
				p[index].setCity(str.nextToken());
				p[index].setState(str.nextToken());
				p[index].setMembertype(str.nextToken());
				sc[index].setMembershipno(str.nextToken());
				sc[index].setOutstandingmovie(Integer.parseInt(str.nextToken()));
				index++;
			}
				}
			}
			if(p!=null && sc!=null)
			{
				System.out.println("print" + p[0].getFirstname() + sc[0].getOutstandingmovie());
				request.setAttribute("personList", p);
				request.setAttribute("customerList", sc);
				request.setAttribute("movie", movie); 
				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ListPerson.jsp");
				dispatcher.forward(request, response);
			}
			else
			{
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/displayAllMovies.jsp");
			dispatcher.forward(request, response);
			}
			
		}
		}
		catch(RemoteException re)
		{
			String message=re.getMessage();
			request.setAttribute("message", message);
			re.printStackTrace();
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/displayAllMovies.jsp");
			dispatcher.forward(request, response);
		}
		catch(Exception e)
		{
			String message=e.getMessage();
			request.setAttribute("message", message);
			e.printStackTrace();
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/displayAllMovies.jsp");
			dispatcher.forward(request, response);
		}
	}

}
